from seaman.core import JsonUtil

from fish.common import FishParams
import math


# 计算掉落电缆的长度
def get_drop_length(params:FishParams) -> float:
    L_c: float = params.L_d - params.L_t
    return L_c


# 计算电缆沿井深分布的轴向压力
# 求出电缆在液体中的重量
def get_cable_weight(params: FishParams) -> float:
    q_l: float = params.q_s - math.pi * 0.25 * math.pow(params.d, 2) * params.g * params.rou_l
    return q_l


# 计算电缆在拉断时的等效弹性模量
def get_cable_elastic_modulus(params: FishParams) -> float:
    cable_area: float = math.pi * math.pow(params.d, 2) * 0.25
    E: float = params.F * 1000 / (params.strain * cable_area)
    return E


class SpiralResult:
    F_hel: float
    Z_hel: float
    L_hel: float


# 确定螺旋段长度
def get_spiral_length(params: FishParams) -> SpiralResult:
    result = SpiralResult()
    # ξ_c≥4.12(q_l/EI)^(1⁄3),电缆为螺旋变形
    ql: float = get_cable_weight(params)
    L_c: float = get_drop_length(params)
    # I为电缆截面惯性距
    I: float = math.pi * math.pow(params.d, 4) / 64
    # 电缆的弹性模量
    params.E: float = get_cable_elastic_modulus(params)
    # 螺旋段临界压力
    epsilon_hel: float = 4.12 * math.pow(ql / (params.E * I), 1/3)
    F_hel: float = epsilon_hel * ql * math.pow((params.E * I) / ql, 1/3)
    result.F_hel = F_hel
    # 3.对应电缆位置
    Z_hel: float = F_hel / ql
    result.Z_hel = Z_hel
    # 4.螺旋段长度
    L_hel: float = L_c - Z_hel
    result.L_hel = L_hel
    return result


# 确定螺旋段区间
def get_spiral_interval(params:FishParams) -> dict[str, float]:
    spiral: SpiralResult = get_spiral_length(params)
    Z_hel: float = spiral.Z_hel
    L_c: float = get_drop_length(params)
    spiral_interval = {}
    spiral_interval['low'] = Z_hel
    spiral_interval['high'] = L_c
    return spiral_interval


class Result:
    p_m: float
    l_m: float
    delta_l: float
    total_l: float
    total_delta_l: float
    m: float


def get_axial_result(params: FishParams) -> Result:
    result = Result()
    ql: float = get_cable_weight(params)
    I: float = math.pi * math.pow(params.d, 4) / 64
    spiral: SpiralResult = get_spiral_length(params)
    params.E: float = get_cable_elastic_modulus(params)

    L_hel: float = spiral.L_hel
    m: float = 1
    # 电缆长度
    total_l: float = 0
    # 总形变量
    total_delta_l: float = 0
    while True:
        if total_l >= L_hel:
            print("螺旋段总变形", total_l)
            break
        else:
            factor_a: float = (9 * math.pow(m, 2) * math.pow(math.pi, 2) * params.E * I) / ql
            factor_b: float = (9 * math.pow(m - 1, 2) * math.pow(math.pi, 2) * params.E * I) / ql
            # 1.第m个螺旋个数的螺距p_m
            p_m: float = math.pow(factor_a, 1 / 3) - math.pow(factor_b, 1 / 3)
            result.p_m = p_m
            # 螺距p_m与螺旋线长度l_m的关系式
            R: float = (params.l - params.d) * 0.5
            factor_c: float = math.pow(2 * math.pi * R, 2)
            factor_d: float = math.pow(p_m, 2)
            l_m: float = math.sqrt(factor_c + factor_d)
            result.l_m = l_m
            # 轴向形变量为
            delta_l: float = l_m - p_m
            result.delta_l = delta_l
            # print(delta_l)
            total_l += l_m
            result.total_l = total_l
            total_delta_l += delta_l
            result.total_delta_l = total_delta_l
            m += 1
            result.m = m
            print("当前m的值为", m)
            print("当前total_l的值为", total_l)
            print("当前total_delta_l的值为", total_delta_l)
    return result


# 获取鱼顶位置
def get_top_position(params:FishParams) -> float:
    axial_result: Result = get_axial_result(params)
    total_delta_l: float = axial_result.total_delta_l
    L_c: float = get_drop_length(params)
    L_p: float =params.L_b - (L_c + params.L_g) + total_delta_l
    return L_p



if __name__ == '__main__':
    params = FishParams()
    # params.L_d = 2000
    # params.L_t = 500
    # params.rou_l = 1300
    # params.q_s = 5.684
    # params.d = 0.0124
    # params.g = 9.8
    # params.E = 51800000000
    # params.l = 0.23
    # params.L_b = 2500
    # params.L_g = 25
    params.L_d = 2000
    params.L_t = 500
    params.rou_l = 1300
    params.q_s = 2.75661
    params.d = 0.008
    params.g = 9.8
    # params.E = 63000000000
    params.l = 0.15
    params.L_b = 2500
    params.L_g = 18.42
    params.strain = 6.69
    params.F = 16700
    result1 = get_drop_length(params)
    print("掉落长度为：", result1)
    result2 = get_cable_weight(params)
    print("电缆在液体中的重量为：", result2)
    result3 = get_cable_elastic_modulus(params)
    print("电缆此时的弹性模量为：", result3)
    spiral_result = get_spiral_length(params)
    print("螺旋段长度为：", JsonUtil.obj2str(spiral_result))
    interval = get_spiral_interval(params)
    print("螺旋段区间为：", interval)
    #
    # result = get_axial_result(params)
    # print(JsonUtil.obj2str(result))
    result_top = get_top_position(params)
    print("鱼顶位置", result_top)

